package BusinessLogic;


import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;


import Data.GiftRecordData;
import Data.GiftRecordDataOperator;
import Data.GoodData;
import Data.GoodData2;
import Data.GoodDataOperator;
import Data.MemberDataOperator;
import Data.MemberData;

public class GiftModel implements IGiftModel{
	
	private MemberDataOperator mDataOpe = new MemberDataOperator();
	private GoodDataOperator gDataOpe=new GoodDataOperator();
	private GiftRecordDataOperator gro = new GiftRecordDataOperator();
	ArrayList<MemberData>  MemberList;
	private GoodData2 gsData;
	int count = 0;

	
	Date dt=new Date();
	SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
	
	

	public ArrayList<GiftRecordData> getGiftRecordData(){
		return gro.getGiftRecordData();
	}

	public MemberData readDataByID(int id){
		
		for(MemberData ud:MemberList){
			if(ud.getMemberID() == id){
				return ud;
			}
		}
		return null;
	}
	
	public boolean saveData(int ID,String name,int goodid,String goodname,double number,String data,double mintegralsum,int isbirthday){
		return gro.saveData(ID, name, goodid, goodname, number, data, mintegralsum, isbirthday);
	}
	
	public boolean birthday(int MemberID) throws ParseException{
		final int millisecondsOfSecond=1000;
		final int secondsOfMin=60;
		final int minsOfHour=60;
		final int hoursOfDay=24;
		Calendar c=Calendar.getInstance();
		c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE));
		String year = String.valueOf(c.get(Calendar.YEAR));
		Date today=c.getTime();
		double time1 = (double) (today.getTime()/millisecondsOfSecond/secondsOfMin/minsOfHour/hoursOfDay);
		GiftModel aa = new GiftModel();
		String mb = aa.getbirthday(MemberID);
		String mb2 = year + "-" + mb;
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");            
        Date date = fmt.parse(mb2);
        double time2 = (double) (date.getTime()/millisecondsOfSecond/secondsOfMin/minsOfHour/hoursOfDay);
        double time = Math.abs(time1-time2);
        if(time<=3 && time>=-3){
        	return true;
        }
        else{
        	return false;
        }

	}
	
	   public String getbirthday(int MemberID){
		   String gb = null;
			   try{
					File f1=new File("D:\\MemberData.txt");
					FileReader fileReader=new FileReader(f1);
					BufferedReader reader=new BufferedReader(fileReader);
					String line=null;
					while((line=reader.readLine())!=null){
						String[] result=line.split(",");
						if(result[0].equals(String.valueOf(MemberID))){
							gb = result[2].substring(5);
						}
				}
					reader.close();
					
				}catch(Exception ex){
				ex.printStackTrace();
				}
				return gb;
		   } 
		
	   public double getMintegralSum(int MemberID){
		   double re=0;
		   try{
				File f1=new File("D:\\MemberData.txt");
				FileReader fileReader=new FileReader(f1);
				BufferedReader reader=new BufferedReader(fileReader);
				String line=null;
				while((line=reader.readLine())!=null){
					String[] result=line.split(",");
					if(result[0].equals(String.valueOf(MemberID))){
						re=re+Double.parseDouble(result[5]);
					}
			}
				
			reader.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
			
			}
		return re;
	   }
	
		public String getMemberNameByID(int ID){
		    String s;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			s = md.getMemberName();
			return s;
		}
		
		public String getMemberBirthdayByID(int ID){
		    String i;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			i = md.getMemberBirthday();
			return i;
		}
		
		public String getMemberSexByID(int ID){
		    String s;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			s = md.getMemberSex();
			return s;
		}
		
		public String getMemberContactWayByID(int ID){
		    String s;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			s = md.getMemberContactWay();
			return s;
		}
		
		public double getMemberIntegralSumByID(int ID){
		    double d;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			d = md.getMemberIntegralSum();
			return d;
		}
		
		public double getMemberIntegralBalanceByID(int ID){
		    double d;
			MemberData md = (MemberData) mDataOpe.readDataByID(ID);
			d = md.getMemberIntegralBalance();
			return d;
		}
		
		public boolean sendGoodByID(int ID,double number){
			GoodDataOperator gd = new GoodDataOperator();
			return gd.sendGoodByID(ID, number);
		}
}
